<template>
	<view class="main">
		<zmm-watermark :watermark="watermark"></zmm-watermark>
		<h2>修改信息</h2>
		<!-- <view class="msg">修改密码后,需要重新登录</view> -->
		<ul class="info">
			<li @tap="upload">
				<view class="msg">
					我的头像
				</view>
				<view class="avatar">
					<image :src="seleUrl" mode=""></image>
				</view>
				<image class="right" src="@/static/img/right.png" mode=""></image>
			</li>
			<li>
				<view class="msg">
					昵称
				</view>
				<view class="txt">
					<input v-model="nickname" type="text" value="" />
				</view>
			</li>
		</ul>
		<u-notify message="APP需要您的同意才能使用相册或摄像头,以便于上传头像" :show="show1" ref="uNotify"></u-notify>
		<view class="btn">
			<view class="in" @tap="confirm">
				确认修改
			</view>
		</view>
	</view>
</template>

<script>
	import permision from "@/js_sdk/wa-permission/permission.js"
	import {
		innerinfo,
		profile
	} from '@/api/inner.js'
	export default {
		data() {
			return {
				nickname: '',
				seleUrl: '',
				url:'',
				watermark: '',
				show1:true
			};
		},
		onLoad() {
			this.getUserInfo()
			this.watermark = uni.getStorageSync('watermark')
		},
		onShow() {
			// this.$refs.uNotify.close()
		},
		methods: {
			async confirm() {
				let res = await profile({
					nickname: this.nickname,
					avatar: this.url
				})
				if (res.code == 1) {
					uni.showToast({
						title: '修改成功',
						icon: 'none'
					})
					setTimeout(() => {
						uni.redirectTo({
							url: '/pages/pageB/data/setUp'
						})
					}, 1000)
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			},
			async upload() {
				this.$refs.uNotify.show({
					top: 0,
					type: 'error',
					color: '#000',
					bgColor: '#fff',
					message: 'APP需要您的同意才能使用相册或摄像头,以便于上传头像',
					duration: 0,
					fontSize: 16,
					safeAreaInsetTop: true
				})
				const systemInfo = uni.getSystemInfoSync()
				if(systemInfo.platform === 'android'){
					let res = await permision.requestAndroidPermission('android.permission.READ_EXTERNAL_STORAGE')
					if(res == 0){
						this.$refs.uNotify.close()
					}
					if(res == -1){
						this.$refs.uNotify.show({
							top: 0,
							type: 'error',
							color: '#000',
							bgColor: '#fff',
							message: 'APP需要您的同意才能使用相册或摄像头,以便于上传头像',
							duration: 0,
							fontSize: 16,
							safeAreaInsetTop: true
						})
						setTimeout(()=>{
							permision.gotoAppPermissionSetting()
						},500)
					}
					if(res == 1){
						this.$refs.uNotify.close()
						uni.chooseImage({
							count: 1,
							sourceType: ['album'], //从相册选择
							success: (res) => {
								this.seleUrl = res.tempFilePaths[0]
								this.uploadFilePromise(this.seleUrl)
							}
						})
					}
				}
				if(systemInfo.platform === 'ios'){
					let res = await permision.judgeIosPermission("photoLibrary")
					if(res){
						this.$refs.uNotify.close()
						uni.chooseImage({
							count: 1,
							sourceType: ['album'], //从相册选择
							success: (res) => {
								this.seleUrl = res.tempFilePaths[0]
								this.uploadFilePromise(this.seleUrl)
							}
						})
					}else{
						setTimeout(()=>{
							permision.gotoAppPermissionSetting()
						},500)
					}
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://app.guoshangufen.com/api/common/upload',
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							res.data = JSON.parse(res.data)
							this.seleUrl = res.data.data.fullurl
							this.url = res.data.data.url
							this.$refs.uNotify.close()
							console.log(this.seleUrl)
						}
					});
				})
			},
			async getUserInfo() {
				let res = await innerinfo()
				this.nickname = res.data.nickname
				this.seleUrl = res.data.avatar
			},
		}
	};
</script>

<style lang="scss" scoped>
	.main {
		padding: 30px 15px 0;

		h2 {
			font-size: 26px;
			color: #232323;
			font-weight: 700;
			margin-bottom: 15px;
		}

		.info {
			width: 100%;
			background-color: #fff;
			margin-bottom: 15px;

			li {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 10px 10px;
				color: #3D3D3D;
				position: relative;

				.avatar {
					image {
						width: 30px;
						height: 30px;
						border-radius: 100px;
					}
				}

				.txt {
					flex: 1;
					color: #3D3D3D;
				}

				.msg {
					margin-right: 20px;
				}

				.right {
					width: 15px;
					height: 15px;
					position: absolute;
					right: 10px;
				}

				input {
					padding: 0 5px;
					height: 40px;
					background-color: #f9f9f9;
					border-radius: 10px;
				}
			}

			li:first-child {
				padding-right: 30px;
			}
		}

		.btn {
			width: 100%;
			padding: 0 50px;

			.in {
				width: 100%;
				padding: 16px;
				text-align: center;
				color: #fff;
				background-color: #0C80FB;
				border-radius: 100px;
			}
		}
	}
</style>